﻿using System;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Space_Conquest
{

    /// <summary>
    /// Holds the Position Information (X, Y) of a Texture to render
    /// </summary>
    class Sprite
    { 

        private  CustomVertex.TransformedColoredTextured [] Vertices;


        private int mX, mY, mWidth, mHeight, mAlpha, mOffsetX, mOffsetY;
        private bool mUseScreenOffset;

        /// <summary>
        /// Minimal Sprite Constructor
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public Sprite(int X, int Y, int Width, int Height):
            this(X, Y, Width, Height, 255, true)
        {
        }


        /// <summary>
        /// Sprite Construtor
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <param name="Alpha"></param>
        /// <param name="UseScreenOffset"></param>
        public Sprite(int X, int Y, int Width, int Height, int Alpha, bool UseScreenOffset)
        {

            mX = X;
            mY = Y;
            mWidth = Width;
            mHeight = Height;
            mAlpha = Alpha;
            mUseScreenOffset = UseScreenOffset;

            Vertices = new CustomVertex.TransformedColoredTextured[4];


        }

        /// <summary>
        /// Renders an entire texture
        /// </summary>
        /// <param name="RenderTexture"></param>
        public void Render(Texture RenderTexture)
        {

            ResetVertex();

            Globals.device.SetTexture(0, RenderTexture.mTexture);

            Globals.device.SetTexture(0, RenderTexture.mTexture);
            Globals.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertices);

        }

        /// <summary>
        /// Renders a section of a texture, based on MapPosition
        /// </summary>
        /// <param name="RenderTexture"></param>
        /// <param name="MapPosition"></param>
        public void Render(Texture RenderTexture, int MapPosition)
        {

            ResetVertexEx(RenderTexture.Width, MapPosition);

            Globals.device.SetTexture(0, RenderTexture.mTexture);
            Globals.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertices);

        }

        /// <summary>
        /// Distance from Left side of screen to Left side of Sprite in Pixels
        /// </summary>
        public int Left
        {
            get
            {
                return mX;
            }
            set
            {
                mX = value;
            }
        }

        /// <summary>
        /// Distance from Left side of screen to Right side of Sprite in Pixels
        /// </summary>
        public int Right
        {
            get
            {
                return mX + mWidth;
            }
            set
            {
                mX = value - mWidth;
            }
        }

        /// <summary>
        /// Distance from Top of screen to Top of Sprite in Pixels
        /// </summary>
        public int Top
        {
            get
            {
                return mY;
            }
            set
            {
                mY = value;
            }
        }

        /// <summary>
        /// Distance from Top of screen to Bottom of Sprite in Pixels
        /// </summary>
        public int Bottom
        {
            get
            {
                return mY + mHeight;
            }
            set
            {
                mY = value - mHeight;
            }

        }

        /// <summary>
        /// Width of Sprite in Pixels
        /// </summary>
        public int Width
        {
            get
            {
                return mWidth;
            }
            set
            {
                mWidth = value;
            }
        }

        /// <summary>
        /// Height of Sprite in Pixels
        /// </summary>
        public int Height
        {
            get
            {
                return mHeight;
            }
            set
            {
                mHeight = value;
            }
        }

        /// <summary>
        /// Transparency of Sprite (255 = Fully shown; 0 = Invisible)
        /// </summary>
        public int Alpha
        {
            get
            {
                return mAlpha;
            }
            set
            {
                mAlpha = value;
            }
        }

        private void ResetVertex()
        {
            if (mUseScreenOffset == true)
            {
                mOffsetX = -Globals.iScreenOffsetX;
                mOffsetY = -Globals.iScreenOffsetY;
            }
            else
            {
                mOffsetX = 0;
                mOffsetY = 0;
            }

            Vertices[0].Position = new Vector4(mX + mOffsetX, mY + mOffsetY, 0, 1);
            Vertices[0].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[0].Tu = 0;
            Vertices[0].Tv = 0;

            Vertices[1].Position = new Vector4(mX + mWidth + mOffsetX, mY + mOffsetY, 0, 1);
            Vertices[1].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[1].Tu = 1;
            Vertices[1].Tv = 0;
                
            Vertices[2].Position = new Vector4(mX + mOffsetX, mY + mHeight + mOffsetY, 0, 1);
            Vertices[2].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[2].Tu = 0;
            Vertices[2].Tv = 1;

            Vertices[3].Position = new Vector4(mX + mWidth + mOffsetX, mY + mHeight + mOffsetY, 0, 1);
            Vertices[3].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[3].Tu = 1;
            Vertices[3].Tv = 1;

        }
        
        private void ResetVertexEx(int TextureWidth, int MapId)
        {
            if (mUseScreenOffset == true)
            {
                mOffsetX = -Globals.iScreenOffsetX;
                mOffsetY = -Globals.iScreenOffsetY;
            }
            else
            {
                mOffsetX = 0;
                mOffsetY = 0;
            }

            double UVWidth = (double)mWidth / (double)TextureWidth;
            int ColumnRows = (int)(1 / UVWidth);

            Vertices[0].Position = new Vector4(mX + mOffsetX, mY + mOffsetY, 0, 1);
            Vertices[0].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[0].Tu = (float)((MapId % ColumnRows) * UVWidth);
            Vertices[0].Tv = (float)((int)(MapId / ColumnRows) * UVWidth);

            Vertices[1].Position = new Vector4(mX + mWidth + mOffsetX, mY + mOffsetY, 0, 1);
            Vertices[1].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[1].Tu = (float)((MapId % ColumnRows) * UVWidth + UVWidth);
            Vertices[1].Tv = (float)((int)(MapId / ColumnRows) * UVWidth);

            Vertices[2].Position = new Vector4(mX + mOffsetX, mY + mHeight + mOffsetY, 0, 1);
            Vertices[2].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[2].Tu = (float)((MapId % ColumnRows) * UVWidth);
            Vertices[2].Tv = (float)((int)(MapId / ColumnRows) * UVWidth + UVWidth);

            Vertices[3].Position = new Vector4(mX + mWidth + mOffsetX, mY + mHeight + mOffsetY, 0, 1);
            Vertices[3].Color = Helpers.ARGB(mAlpha, 255, 255, 255);
            Vertices[3].Tu = (float)((MapId % ColumnRows) * UVWidth + UVWidth);
            Vertices[3].Tv = (float)((int)(MapId / ColumnRows) * UVWidth + UVWidth);

        }


    }
}
